package com.manager.devicemanager.service.impl;

import com.manager.devicemanager.entity.UserInfo;
import com.manager.devicemanager.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserInfoMapper userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo user = userRepository.selectByUsername(username);
        System.out.println(user);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在 " + username);
        }


        List<GrantedAuthority> authorities = new ArrayList<>();
        // 假设 UserInfo 类有一个 role 字段
//        authorities.add(new SimpleGrantedAuthority("ROLE_" + userInfo.getRole()));

        return new User(
                user.getUsername(),
                user.getPassword(),
                authorities
        );

    }
}